package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.StaffInfoManageMapper;
import com.sinog.biz.mapper.havexml.SystemCodeMapper;
import com.sinog.biz.service.business.StaffInfoManageService;
import com.sinog.biz.util.IdGen;
import com.sinog.biz.util.smlib.Sm4;
import com.sinog.core.domain.StaffInfoManage;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.PageReturnUtil;
import com.sinog.core.util.PhoneUtil;
import com.sinog.core.util.StrUtil;
import com.sinog.core.utilbean.Constants;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 安帮工作资源管理 - 安帮工作人员信息管理 - 安帮工作人员信息管理 StaffInfoManageServiceImpl
 * @author dangyf
 * @date 2020年2月5日 20点48分
 */
@Service
public class StaffInfoManageServiceImpl extends ServiceImpl<StaffInfoManageMapper,StaffInfoManage> implements StaffInfoManageService {

    @Resource
    private StaffInfoManageMapper staffInfoManageMapper;
    @Resource
    private SystemCodeMapper systemCodeMapper;

    /**
     * 安帮工作资源管理 - 安帮工作人员信息管理 - 安帮工作人员信息管理 - 列表
     * @param searchKey searchKey
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param staffTypeSearch staffTypeSearch
     * @param departId departId
     * @return String
     */
    @Override
    public String getStaffInfoList(String searchKey,Integer pageIndex,Integer pageSize,String staffTypeSearch,String departId) {
        //参数转换
        searchKey = StrUtil.converseToNull(searchKey);
        staffTypeSearch = StrUtil.converseToNull(staffTypeSearch);
        //分页查询
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        List<Map<String,String>> list = staffInfoManageMapper.getStaffInfoList(page,searchKey,staffTypeSearch,departId);
        //处理列表
        for(Map<String,String> map : list) {
            if(null != map.get("lxfs")) {
                Map<String,String> telAndPhone = PhoneUtil.splitTelAndPhone(map.get("lxfs"));
                map.put("lxfs",telAndPhone.get("phone"));
                map.put("tel",telAndPhone.get("tel"));
            }
        }
        page = page.setRecords(list);
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 安帮工作资源管理 - 安帮工作人员信息管理 -（新增/更新）
     * @param data data
     * @param loginUserParam loginUserParam
     * @return Result
     */
    @Override
    public Result save(String data,Map<String,String> loginUserParam) {
        //实体参数
        StaffInfoManage staffInfoManage = JsonUtil.toBean(data,StaffInfoManage.class);
        // 身份证加密
        staffInfoManage.setSfzh(Sm4.encryptEcb(staffInfoManage.getSfzh()));
        //对只有电话/只有固话的进行|的去除
        String lxfs = staffInfoManage.getLxfs();
        if (StringUtils.isNotBlank(lxfs)) {
            Character one = lxfs.charAt(0);
            Character last = lxfs.charAt(lxfs.length()-1);
            if ("|".equals(one.toString()) || "|".equals(last.toString())) {
                String lxfs1 = lxfs.replace("|","");
                staffInfoManage.setLxfs(lxfs1);
            }
        }
        Date date = new Date();
        // 设置住址省、市、县、街道名称
        String provinceCode = staffInfoManage.getProvince();
        String cityCode = staffInfoManage.getCity();
        String countyCode = staffInfoManage.getCounty();
        String villageCode = staffInfoManage.getVillage();
        staffInfoManage.setProvinceName(null == systemCodeMapper.getNameByCode(provinceCode) ? "" : systemCodeMapper.getNameByCode(provinceCode));
        staffInfoManage.setCityName((null == systemCodeMapper.getNameByCode(cityCode) ? "" : systemCodeMapper.getNameByCode(cityCode)));
        staffInfoManage.setCountyName((null == systemCodeMapper.getNameByCode(countyCode) ? "" : systemCodeMapper.getNameByCode(countyCode)));
        staffInfoManage.setVillageName(null == systemCodeMapper.getNameByCode(villageCode) ? "" : systemCodeMapper.getNameByCode(villageCode));
        staffInfoManage.setBffwCode(Constants.NumberStr.NUM_STR_FU_ONE.equals(staffInfoManage.getBffwCode()) ? null : staffInfoManage.getBffwCode());
        boolean mark = StringUtils.isBlank(staffInfoManage.getId());
        //新增
        if(mark) {
            staffInfoManage.setId(IdGen.strid());
            staffInfoManage.setClab(loginUserParam.get("departId"));
            staffInfoManage.setClabName(loginUserParam.get("departName"));
            staffInfoManage.setCreateUser(loginUserParam.get("loginUser"));
            staffInfoManage.setCreateTime(date);
        }
        staffInfoManage.setUpdateUser(loginUserParam.get("loginUser"));
        staffInfoManage.setUpdateTime(date);
        boolean insertOrUpdateFlag = staffInfoManage.insertOrUpdate();
        if(insertOrUpdateFlag) {
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 安帮工作人员信息管理 - 删除安帮工作人员信息
     * @param id id
     * @return Result
     */
    @Override
    public Result deleteOne(String id) {
        StaffInfoManage staffInfoManage = new StaffInfoManage();
        boolean result = staffInfoManage.deleteById(id);
        if(result) {
            return Result.success("删除成功！");
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 安帮工作人员信息管理 - 安帮工作人员信息管理 - 导出列表数据
     * @param departId departId
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> selectStaffInfoForExport(String departId) {
        return staffInfoManageMapper.selectStaffInfoForExport(departId);
    }

    /**
     * 安帮工作资源管理 - 安帮工作人员信息管理 - 安帮工作人员信息管理 - 查看
     * @param id id
     * @return Result
     */
    @Override
    public Result selectStaffInfoManage(String id) {
        return Result.success(staffInfoManageMapper.selectById(id));
    }
}